(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.group.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'groupService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, groupService) {
		var ctrl = this, groupInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.group = {};
		ctrl.init = function() {
			ctrl.accessRolePermission = {permission : 'accessRole', authorization : authService};
			ctrl.accessBuPermission =  {permission : 'accessBu', authorization : authService};
			// init group data
			groupService.initPrincipalRoles($scope, ctrl);
			groupService.initParentGroups($scope, ctrl);
			groupService.initSubGroups($scope, ctrl);
			groupService.initGroupUsers($scope, ctrl);
			groupService.initBus($scope, ctrl);
			ctrl.parentGroupGridOptions.enableRowHeaderSelection = false;
			ctrl.principalRoleGridOptions.enableRowHeaderSelection = false;
			ctrl.subGroupGridOptions.enableRowHeaderSelection = false;
			ctrl.userGridOptions.enableRowHeaderSelection = false;
			ctrl.buGridOptions.enableRowHeaderSelection = false;
			
			return authService.view(groupInfo).then(function(response) {
				ctrl.group = response.data || groupInfo;
				ctrl.group.noteMsgs = ctrl.group.noteMsgs || [];
				ctrl.getPrincipalRoleList();
				ctrl.getParentGroupList();
				ctrl.getSubGroupList();
				ctrl.getGroupUserList();
				
				groupService.getParentCompanyCodeOptions(ctrl).then(function(response) {
					ctrl.companyCodeOptions = response;
				}, angular.noop);
			}, angular.noop);
		};
		
		ctrl.getPrincipalRoleList = function() {
			authService.getPrincipalRoleList(groupInfo).then(function(response) {
				ctrl.group.principalRoles = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.principalRoles, ctrl.principalRoleGridOptions);
			});
		};
		
		ctrl.getParentGroupList = function() {
			authService.getParentGroupList(groupInfo).then(function(response) {
				ctrl.group.parentGroups = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.parentGroups, ctrl.parentGroupGridOptions);
			});
		};
		
		ctrl.getSubGroupList = function() {
			authService.getSubGroupList(groupInfo).then(function(response) {
				ctrl.group.subGroups = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.subGroups, ctrl.subGroupGridOptions);
			});
		};
		
		ctrl.getGroupUserList = function() {
			authService.getGroupUserList(groupInfo).then(function(response) {
				ctrl.group.groupUsers = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.groupUsers, ctrl.userGridOptions);
			});
		};
		
	}
})(angular);